<template>
  <view class="page-container">
    <view class="form-wrapper">
      <view class="cell-column">
        <view class="title">医师执业证</view>
        <view class="input-wrapper" @tap="jumpAuthPage('physicianLicense')">
          <view class="uni-input" :class="{done: physicianLicense === '3', nopass: physicianLicense ==='4'}">{{statusMap[physicianLicense]}}</view>
          <image class="suffix-arrow" src="../../../static/img/user/arrow.png" mode="widthFix"></image>
        </view>
      </view>
      <view class="cell-column">
        <view class="title">工作证/工牌</view>
        <view class="input-wrapper" @tap="jumpAuthPage('workPermit')">
          <view class="uni-input" :class="{done: workPermit === '3', nopass: workPermit ==='4'}">{{statusMap[workPermit]}}</view>
          <image class="suffix-arrow" src="../../../static/img/user/arrow.png" mode="widthFix"></image>
        </view>
      </view>
      <view class="cell-column">
        <view class="title">身份证</view>
        <view class="input-wrapper" @tap="jumpAuthPage('idCard')">
          <view class="uni-input" :class="{done: idCard === '3', nopass: idCard ==='4'}">{{statusMap[idCard]}}</view>
          <image class="suffix-arrow" src="../../../static/img/user/arrow.png" mode="widthFix"></image>
        </view>
      </view>
      <view class="cell-column">
        <view class="title">半身照</view>
        <view class="input-wrapper" @tap="jumpAuthPage('halfLength')">
          <view class="uni-input" :class="{done: halfLength === '3', nopass: halfLength ==='4'}">{{statusMap[halfLength]}}</view>
          <image class="suffix-arrow" src="../../../static/img/user/arrow.png" mode="widthFix"></image>
        </view>
      </view>
    </view>
    <view class="page-footer">
		<view style="text-align: center; width: 100vw; padding-bottom: 20rpx;" @tap="callPhone" >联系客服：17769537971（微信同号）</view>
      <button v-if="showBtn" class="btn-save" type="primary" @tap="handleSave">保存</button>
    </view>
  </view>
</template>

<script>
  import {
    getHcp,
    postProfile
  } from '@/utils/api';
  export default {
    data() {
      return {
        physicianLicense: '1', // 医生执业证
        workPermit: '1', // 工作证/工牌
        idCard: '1', // 身份证
        halfLength: '1', // 半身照
        statusMap: {
          '1': '待上传',
          '2': '审核中',
          '3': '已完成',
          '4': '未通过',
          '99': '已上传'
        }
      }
    },
    computed: {
      showBtn: function() {
        const keys = ['physicianLicense', 'workPermit', 'idCard', 'halfLength'];
        let flag = false;
        for (let i = 0; i < keys.length; i++) {
          let status = this[keys[i]];
          console.log(status)
          flag = flag || (status === '1' || status === '99' || status === '4')
        }
        console.log(flag)
        return flag;
      },
      enabledBtn: function() {
        const keys = ['physicianLicense', 'workPermit', 'idCard', 'halfLength'];
        let flag = true;
        for (let i = 0; i < keys.length; i++) {
          flag = flag && this[keys[i]] !== '1'
        }
        return flag;
      }
    },
    onShow() {
      this.queryUser();
    },
    methods: {
		callPhone(){
			uni.makePhoneCall({
			    phoneNumber: '17769537971',
				success: (res) => {
					console.log('调用成功!')	
				},
				fail: (res) => {
					console.log('调用失败!')
				}
			  });
		},
      // 查询用户信息
      async queryUser() {
        let res = await getHcp();
        const userInfo = res.data;
        const identification = userInfo.identification;
        // 医生执业证
        this.physicianLicense = this.setAuthStatus(identification, 'physicianLicense');
        // 工作证/工牌
        this.workPermit = this.setAuthStatus(identification, 'workPermit');
        // 身份证
        this.idCard = this.setAuthStatus(identification, 'idCard');
        // 半身照
        this.halfLength = this.setAuthStatus(identification, 'halfLength');
      },
      // 设置认证状态
      setAuthStatus(identification, type) {
        let status;
        let store = uni.getStorageSync('auth_' + type);
        if (store) {
          status = store; // 已上传
        } else {
          if (identification && identification[type]) {
            status = identification[type].status;
            if (status === 'waiting') {
              status = '2'; // 审核中
            } else if (status === 'approved') {
              status = '3'; // 已完成
            } else if (status === 'rejected') {
              status = '4'; // 未通过
            }
          } else {
            status = '1'; // 待上传
          }
        }
        return status;
      },

      // 跳转到认证页面
      jumpAuthPage(type) {
        let url;
        let status;
        switch (type) {
          case 'physicianLicense':
            status = this.physicianLicense;
            uni.removeStorageSync('auth_physicianLicense');
            url = '/pages/user/info/doctorcard';
            break;
          case 'workPermit': // 工作证/工牌
            status = this.workPermit;
            uni.removeStorageSync('auth_workPermit');
            url = '/pages/user/info/workcard';
            break;
          case 'idCard':
            status = this.idCard;
            uni.removeStorageSync('auth_idCard');
            url = '/pages/user/info/idcard';
            break;
          case 'halfLength':
            status = this.halfLength;
            uni.removeStorageSync('auth_halfLength');
            url = '/pages/user/info/bust';
            break;
        }
        console.log(type + ' : ' + status)
        // 审核中、已完成不允许跳转到上传认证图片页面
        if (status == '2' || status === '3' || status === '99') {
          return
        }
        uni.navigateTo({
          url: url
        });
      },
      // 保存
      async handleSave() {
        if (!this.enabledBtn) {
          uni.showToast({
            title: '您还有资料未上传！',
            icon: 'none',
            duration: 2000
          });
          return;
        }
        // const base = uni.getStorageSync('userAuth');
        // const res = await postAccount(base);
        let identities = uni.getStorageSync('identities');
        let res = await postProfile({
          identities: identities
        });
        if (res.status === 0) {
          uni.removeStorageSync('identities');
          uni.showToast({
            title: '完善资料成功！',
            icon: 'none',
            duration: 2000
          });

          setTimeout(function() {
            uni.switchTab({
              url: '/pages/tabbar/my/my'
            })
          }, 2000);
        } else {
          // 提交失败
          uni.showToast({
            title: res.message,
            icon: 'none',
            duration: 2000
          });
        }
      }
    }
  }
</script>

<style>
  @import url("../../../static/style/user.css");

  page {
    background: #F4F4F4;
  }

  .page-container {
    display: flex;
    height: 100%;
    flex-direction: column;
    justify-content: space-between;
  }

  .form-wrapper {
    background: #FFFFFF;
  }

  .cell-column {
    height: 108upx;
    border-bottom: 2upx solid #F4F4F4;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 30upx;
  }

  .title {
    font-size: 30upx;
    color: #999;
  }

  .input-wrapper {
    display: flex;
    align-items: center;
  }

  .uni-input {
    text-align: right;
    font-size: 30upx;
    color: #333;
    padding-right: 10upx;
  }

  .uni-input.done {
    color: #AD85FC;
  }

  .uni-input.nopass {
    color: #F76260;
  }

  .suffix {
    width: 40upx;
    height: 40upx;
  }

  .suffix-arrow {
    margin-left: 16upx;
    width: 24upx;
    height: 44upx;
  }
</style>
